package com.zh.note.leetcode.greedy;

/**
 * 738. 单调递增的数字
 * <p>
 * 输入: n = 1234
 * 输出: 1234
 */
public class LC_738_monotoneIncreasingDigits {

    public static int monotoneIncreasingDigits(int n) {

        String str = String.valueOf(n);
        char[] chars = str.toCharArray();
        // flag用来标记赋值9从哪里开始
        int flag = str.length();
        // 从后往前遍历
        for (int i = chars.length - 1; i > 0; i--) {
            if (chars[i - 1] > chars[i]) {
                chars[i - 1]--;
                flag = i;
            }
        }
        for (int i = flag; i < chars.length; i++) {
            chars[i] = '9';
        }

        String res = String.valueOf(chars);
        return Integer.parseInt(res);
    }

    public static void main(String[] args) {
        System.out.println(monotoneIncreasingDigits(10));
    }

}
